#!/bin/bash

KERNEL='./Image'
BIOSFW='./fw_jump.elf'
ROOTFS='./rootfs.ext2'
QEMUVM='/opt/riscv64/bin/qemu-system-riscv64'

check_file() {
	if [ ! -f "$1" ] ; then
		echo "Error, file not found: \"$1\"." 1>&2
		return 1
	fi
	return 0
}

check_file "${KERNEL}" || exit 1
check_file "${BIOSFW}" || exit 2
check_file "${ROOTFS}" || exit 3
check_file "${QEMUVM}" || exit 4

exec "${QEMUVM}" -M virt -bios "${BIOSFW}" -kernel "${KERNEL}" \
	-smp 4 -m 512M -append "rootwait root=/dev/vda ro nohz=0" \
	-drive "file=${ROOTFS},format=raw,id=hd0" \
	-device 'virtio-blk-device,drive=hd0' \
	-netdev 'user,id=net0' -device 'virtio-net-device,netdev=net0' \
	-nographic -gdb 'tcp::2023' -S
exit $?
